/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TransportesFrame.java
 *
 * Created on 30-abr-2011, 19:08:00
 */

package ia_psr.transportesGUI;

/**
 *
 * @author Rubén Avalos Elvira y Fabiana Celiberti Tapie
 */
import ia_psr.PSRSolver;
import java.awt.BorderLayout;
import ia_psr.fileparser.PSRFileParser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import java.io.File;
import javax.swing.JPanel;

public class TransportesFrame extends JFrame {
    ConductoresPanel conductoresPanel;
    TareasPanel tareasPanel;
    AlgoritmosPanel algoritmosPanel;
    GeneracionAleatoriaPanel aleatoriosPanel;
    HorarioEmpresaPanel horarioPanel;
    PlanificacionPanel planPanel;
    InfoAdicionalPanel infoPanel;
    PSRFileParser parser;
    PSRSolver solver;

    /** Creates new form TransportesFrame */
    public TransportesFrame() {
        parser           = new PSRFileParser();
        conductoresPanel = new ConductoresPanel();
        tareasPanel      = new TareasPanel();
        algoritmosPanel  = new AlgoritmosPanel(this);
        aleatoriosPanel  = new GeneracionAleatoriaPanel();
        horarioPanel     = new HorarioEmpresaPanel();
        planPanel        = new PlanificacionPanel();
        infoPanel        = new InfoAdicionalPanel();

        initComponents();

        JPanel datosTab = new JPanel();
        datosTab.setLayout(new BorderLayout());

        datosTab.add(conductoresPanel, BorderLayout.WEST);
        datosTab.add(tareasPanel,      BorderLayout.CENTER);

        JPanel paux = new JPanel();
        paux.setLayout(new BorderLayout());
        paux.add(horarioPanel, BorderLayout.WEST);
        paux.add(algoritmosPanel, BorderLayout.CENTER);
        datosTab.add(paux, BorderLayout.NORTH);

        tp.addTab("Empresa", datosTab);

        JPanel planningTab = new JPanel();
        planningTab.setLayout(new BorderLayout());
        // añadir elementos al panel de resultados
        planningTab.add(infoPanel, BorderLayout.NORTH);
        planningTab.add(planPanel, BorderLayout.CENTER);

        tp.addTab("Planificación", planningTab);

        setVisible(true);
        pack();
    }

    public void presentPlanning(double tiempo, int nodos, int variables) {
        planPanel.populatePlanTable();
        infoPanel.setInfo(tiempo, nodos, variables);
        tp.setSelectedIndex(tp.indexOfTab("Planificación"));

    }

    public void clearPlanningData() {
        planPanel.clearData();
        infoPanel.clearData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        tp = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        miAbrir = new javax.swing.JMenuItem();
        miGuardar = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        miSalir = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        miCrearConductor = new javax.swing.JMenuItem();
        miEliminarConductor = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        miCrearTarea = new javax.swing.JMenuItem();
        miEliminarTarea = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        miSetHorario = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JPopupMenu.Separator();
        miConductoresAleatorios = new javax.swing.JMenuItem();
        miTareasAleatorias = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Enterprise TaskScheduler v1.0");
        setResizable(false);

        jMenu1.setText("Archivo");

        miAbrir.setText("Abrir escenario");
        miAbrir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                miAbrirMouseReleased(evt);
            }
        });
        jMenu1.add(miAbrir);

        miGuardar.setText("Guardar escenario");
        miGuardar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                miGuardarMouseReleased(evt);
            }
        });
        jMenu1.add(miGuardar);
        jMenu1.add(jSeparator1);

        miSalir.setText("Salir");
        miSalir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                miSalirMouseReleased(evt);
            }
        });
        jMenu1.add(miSalir);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edición");

        miCrearConductor.setText("Crear conductor");
        miCrearConductor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                miCrearConductorMouseReleased(evt);
            }
        });
        jMenu2.add(miCrearConductor);

        miEliminarConductor.setText("Eliminar conductor");
        miEliminarConductor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                miEliminarConductorMouseReleased(evt);
            }
        });
        jMenu2.add(miEliminarConductor);
        jMenu2.add(jSeparator2);

        miCrearTarea.setText("Crear tarea");
        miCrearTarea.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                miCrearTareaMouseReleased(evt);
            }
        });
        jMenu2.add(miCrearTarea);

        miEliminarTarea.setText("Eliminar tarea");
        miEliminarTarea.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                miEliminarTareaMouseReleased(evt);
            }
        });
        jMenu2.add(miEliminarTarea);
        jMenu2.add(jSeparator3);

        miSetHorario.setText("Establecer horario de la empresa");
        miSetHorario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                miSetHorarioMouseReleased(evt);
            }
        });
        jMenu2.add(miSetHorario);
        jMenu2.add(jSeparator4);

        miConductoresAleatorios.setText("Generar conductores aleatorios");
        miConductoresAleatorios.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                miConductoresAleatoriosMouseReleased(evt);
            }
        });
        jMenu2.add(miConductoresAleatorios);

        miTareasAleatorias.setText("Generar tareas aleatorias");
        miTareasAleatorias.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                miTareasAleatoriasMouseReleased(evt);
            }
        });
        jMenu2.add(miTareasAleatorias);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tp, javax.swing.GroupLayout.DEFAULT_SIZE, 769, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tp, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void miAbrirMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_miAbrirMouseReleased
        JFileChooser fc = new JFileChooser();
        int seleccion = fc.showOpenDialog(null);
        if (seleccion == JFileChooser.APPROVE_OPTION) {
            File fichero = fc.getSelectedFile();
            parser.parse(fichero.getAbsolutePath());
            // se actualiza la interface
            conductoresPanel.clearConductoresList();
            tareasPanel.clearTareasTable();
            
            conductoresPanel.populateConductoresList();
            tareasPanel.populateTareasTable();
        }
    }//GEN-LAST:event_miAbrirMouseReleased

    private void miCrearConductorMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_miCrearConductorMouseReleased
        conductoresPanel.showAddConductorDialog();
    }//GEN-LAST:event_miCrearConductorMouseReleased

    private void miEliminarConductorMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_miEliminarConductorMouseReleased
        conductoresPanel.removeSelectedConductor();
    }//GEN-LAST:event_miEliminarConductorMouseReleased

    private void miCrearTareaMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_miCrearTareaMouseReleased
        tareasPanel.showAddTareaDialog();
    }//GEN-LAST:event_miCrearTareaMouseReleased

    private void miEliminarTareaMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_miEliminarTareaMouseReleased
        tareasPanel.removeSelectedTarea();
    }//GEN-LAST:event_miEliminarTareaMouseReleased

    private void miSalirMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_miSalirMouseReleased
        System.exit(0);
    }//GEN-LAST:event_miSalirMouseReleased

    private void miConductoresAleatoriosMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_miConductoresAleatoriosMouseReleased
        JFrame frame = new ConductoresAleatoriosDialog(conductoresPanel);
        frame.setVisible(true);
    }//GEN-LAST:event_miConductoresAleatoriosMouseReleased

    private void miTareasAleatoriasMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_miTareasAleatoriasMouseReleased
        JFrame frame = new TareasAleatoriasDialog(tareasPanel);
        frame.setVisible(true);
    }//GEN-LAST:event_miTareasAleatoriasMouseReleased

    private void miSetHorarioMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_miSetHorarioMouseReleased
        JFrame frame = new SetHorarioEmpresaDialog(horarioPanel);
        frame.setVisible(true);
    }//GEN-LAST:event_miSetHorarioMouseReleased

    private void miGuardarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_miGuardarMouseReleased
        JFileChooser fc = new JFileChooser();
        int seleccion = fc.showSaveDialog(null);
        if (seleccion == JFileChooser.APPROVE_OPTION) {
            File fichero = fc.getSelectedFile();
            parser.savePSR(fichero);
        }
    }//GEN-LAST:event_miGuardarMouseReleased

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TransportesFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JPopupMenu.Separator jSeparator4;
    private javax.swing.JMenuItem miAbrir;
    private javax.swing.JMenuItem miConductoresAleatorios;
    private javax.swing.JMenuItem miCrearConductor;
    private javax.swing.JMenuItem miCrearTarea;
    private javax.swing.JMenuItem miEliminarConductor;
    private javax.swing.JMenuItem miEliminarTarea;
    private javax.swing.JMenuItem miGuardar;
    private javax.swing.JMenuItem miSalir;
    private javax.swing.JMenuItem miSetHorario;
    private javax.swing.JMenuItem miTareasAleatorias;
    private javax.swing.JTabbedPane tp;
    // End of variables declaration//GEN-END:variables

}
